Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Operators

Bitwise operators

Bitwise operators work directly on the binary representation of numbers. In Python, integers are typically stored in memory using 32 or 64 bits. These operators manipulate the individual bits within those numbers, enabling low-level programming tasks and creating special effects.

Types of Bitwise operators

Bitwise AND (&)

Performs a bit-by-bit AND operation on the corresponding bits of two operands. Resulting bit is 1 only if both bits at that position in the operands are 1. Example:
Bitwise AND (&) basic example in python x = 10 #(binary: 1010) y = 6 #(binary: 0110) z = x & y # z will be 2 (binary: 0010) print(x) print(y) print(z)

Output

10 6 2

Bitwise OR (|)

Performs a bit-by-bit OR operation on the corresponding bits of two operands. Resulting bit is 1 if at least one bit at that position in the operands is 1. Example:
Bitwise OR (|) basic example in python x = 10 #(binary: 1010) y = 6 #(binary: 0110) z = x | y # z will be 14 (binary: 1110) print(x) print(y) print(z)

Output

10 6 14

Bitwise XOR (^)

Performs a bit-by-bit XOR (exclusive OR) operation on the corresponding bits of two operands. Resulting bit is 1 if the bits at that position in the operands are different. Example:
Bitwise XOR (^) basic example in python x = 10 #(binary: 1010) y = 6 #(binary: 0110) z = x ^ y # z will be 12 (binary: 1100) print(x) print(y) print(z)

Output

10 6 12

Left Shift (<<)

Shifts the bits of the left operand to the left by the number of bits specified by the right operand. Zeros are filled in on the right side. Example:
Bitwise Left Shift (<<) basic example in python x = 5 #(binary: 0101) y = 2 # shift by 2 bits z = x << y # z will be 20 (binary: 10100) print(x) print(y) print(z)

Output

5 2 20

Right Shift (>>)

Shifts the bits of the left operand to the right by the number of bits specified by the right operand. The sign bit is either propagated (signed right shift) or filled with zeros (unsigned right shift), depending on the implementation. Python uses arithmetic right shift by default. Example (assuming arithmetic right shift):
Bitwise Right Shift (>>) basic example in python x = -10 #(binary: 11111111111111010) Assuming 32-bit integers y = 2 # shift by 2 bits z = x >> y # z will be -2 (binary: 1111111111111110) print(x) print(y) print(z)

Output

-10 2 -3

Not (~)

the tilde (~) operator is used for the bitwise NOT operation, which flips the bits of a number. It's not related to the logical NOT operator (not) used for negating Boolean values. Bitwise NOT (~): Flips the bits of a number. For example, ~5 (binary: 0101) would result in -6 (binary: 1010) in Python (assuming two's complement representation for negative integers). Logical NOT (not): Negates a Boolean value. For example, not True is False and not False is True. Basic example:
Bitwise NOT (~) basic example in python x=10 print(x) print(~x)

Output

10 -11

Use Cases for Bitwise Operators

Extracting bits: Isolate specific bits from a number using masking techniques with bitwise AND. ⮞ Setting/clearing bits: Set or clear individual bits within a number using bitwise OR and bitwise AND with appropriate masks. ⮞ Creating flags: Combine multiple flags (represented as bits) into a single integer. ⮞ Low-level graphics programming: Manipulate pixel colors by working with their binary representations.

  📌TAGS

★python ★operators ★Bitwise

Tutorials